﻿#Region "Imports"

Imports Odyssey.Controls
Imports Microsoft.WindowsAPICodePack
Imports MS.WindowsAPICodePack.Internal.CoreHelpers
Imports Microsoft.WindowsAPICodePack.Shell
Imports Microsoft.WindowsAPICodePack.Taskbar

#End Region

Public Class DocumentTab
    Inherits TabItem

#Region "Publics"

    Public DocumentName As String = Nothing
    Public DocName As String = Nothing
    Public FileChanged As Boolean = False
    Public FileFormat As String = "Rich Text Document"

    Public Event Close As RoutedEventHandler
    Public Shared UpdateButtons As RoutedEvent = EventManager.RegisterRoutedEvent("UpdateButtons", RoutingStrategy.Tunnel, GetType(RoutedEventHandler), GetType(DocumentTab))

    Public Shared UndoEvent As RoutedEvent = EventManager.RegisterRoutedEvent("Undo", RoutingStrategy.Tunnel, GetType(RoutedEventHandler), GetType(DocumentTab))
    Public Shared RedoEvent As RoutedEvent = EventManager.RegisterRoutedEvent("Redo", RoutingStrategy.Tunnel, GetType(RoutedEventHandler), GetType(DocumentTab))
    Public Shared CutEvent As RoutedEvent = EventManager.RegisterRoutedEvent("Cut", RoutingStrategy.Tunnel, GetType(RoutedEventHandler), GetType(DocumentTab))
    Public Shared CopyEvent As RoutedEvent = EventManager.RegisterRoutedEvent("Copy", RoutingStrategy.Tunnel, GetType(RoutedEventHandler), GetType(DocumentTab))
    Public Shared PasteEvent As RoutedEvent = EventManager.RegisterRoutedEvent("Paste", RoutingStrategy.Tunnel, GetType(RoutedEventHandler), GetType(DocumentTab))
    Public Shared DeleteEvent As RoutedEvent = EventManager.RegisterRoutedEvent("Delete", RoutingStrategy.Tunnel, GetType(RoutedEventHandler), GetType(DocumentTab))
    Public Shared SelectAllEvent As RoutedEvent = EventManager.RegisterRoutedEvent("SelectAll", RoutingStrategy.Tunnel, GetType(RoutedEventHandler), GetType(DocumentTab))
    Public Shared FindEvent As RoutedEvent = EventManager.RegisterRoutedEvent("Find", RoutingStrategy.Tunnel, GetType(RoutedEventHandler), GetType(DocumentTab))
    Public WithEvents HeaderContent As New DocBox
    Public WithEvents TBox As New RichTextBox
    Public Title As New Label
    Public MyIndex As Integer
    Public WithEvents TBoxContextMenu As ContextMenu
    Public WithEvents UndoMenuItem As New RibbonMenuItem
    Public WithEvents RedoMenuItem As New RibbonMenuItem
    Public WithEvents CutMenuItem As New RibbonMenuItem
    Public WithEvents CopyMenuItem As New RibbonMenuItem
    Public WithEvents PasteMenuItem As New RibbonMenuItem
    Public WithEvents DeleteMenuItem As New RibbonMenuItem
    Public WithEvents SelectAllMenuItem As New RibbonMenuItem
    Public WithEvents FindMenuItem As New RibbonMenuItem

#End Region

#Region "FileBox"

    Public Sub New(ByVal name As String)
        If RunningOnVista Or RunningOnWin7 Then
            If My.Settings.Options_IsGlassOn Then
                Me.Background = Brushes.Transparent
            End If
        End If
        DocName = name
        HeaderContent.TabTitle.Content = name
        HeaderContent.TabTitle.ToolTip = name
        Me.Header = HeaderContent
        Me.Height = 26
        If My.Settings.Options_SpellCheck Then
            SpellCheck.SetIsEnabled(TBox, True)
        End If
        TBox.HorizontalAlignment = Windows.HorizontalAlignment.Stretch
        TBox.VerticalAlignment = Windows.VerticalAlignment.Stretch
        Content = TBox
        TBox.VerticalScrollBarVisibility = ScrollBarVisibility.Auto
        TBoxContextMenu = New ContextMenu
        UndoMenuItem.Header = "Undo"
        RedoMenuItem.Header = "Redo"
        CutMenuItem.Header = "Cut"
        CopyMenuItem.Header = "Copy"
        PasteMenuItem.Header = "Paste"
        DeleteMenuItem.Header = "Delete"
        SelectAllMenuItem.Header = "Select All"
        TBoxContextMenu.Items.Add(UndoMenuItem)
        TBoxContextMenu.Items.Add(RedoMenuItem)
        TBoxContextMenu.Items.Add(New Separator)
        TBoxContextMenu.Items.Add(CutMenuItem)
        TBoxContextMenu.Items.Add(CopyMenuItem)
        TBoxContextMenu.Items.Add(PasteMenuItem)
        TBoxContextMenu.Items.Add(DeleteMenuItem)
        TBoxContextMenu.Items.Add(New Separator)
        TBoxContextMenu.Items.Add(SelectAllMenuItem)
        TBoxContextMenu.Background = Brushes.White
        TBox.ContextMenu = TBoxContextMenu
    End Sub

    Private Sub FileBox_SizeChanged(ByVal sender As Object, ByVal e As System.Windows.SizeChangedEventArgs) Handles Me.SizeChanged
        If RunningOnVista Or RunningOnWin7 Then
            If HeaderContent.Width > 32 Then
                'HeaderContent.Width = Width - 14
            End If
        Else 'Windows XP
            If HeaderContent.Width > 32 Then
                'HeaderContent.Width = Width - 28
            End If
        End If
    End Sub

#End Region

#Region "Header"

    Public Sub SetFileType(ByVal s As String)
        HeaderContent.FileTypeImage.ToolTip = s
        FileFormat = s
        If Not s = "Rich Text Document" Then
            HeaderContent.FileTypeImage.Source = New BitmapImage(New Uri(My.Application.Info.DirectoryPath + "\Images\Tab\txt.png"))
        End If
    End Sub

    Public Sub SetDocumentTitle(ByVal title As String)
        HeaderContent.TabTitle.Content = title
        DocName = title
    End Sub

    Public Sub ShowCloseButton()
        If My.Settings.Options_Tabs_CloseButtonMode = 0 Then
            HeaderContent.CloseButton.Visibility = Windows.Visibility.Visible
        Else
            If Me.IsSelected Then
                HeaderContent.SetMenu(True)
                HeaderContent.CloseButton.Visibility = Windows.Visibility.Visible
            Else
                HeaderContent.SetMenu(False)
                HeaderContent.CloseButton.Visibility = Windows.Visibility.Hidden
            End If
        End If
    End Sub

#End Region

#Region "TBox"

#Region "Context Menu"

    Private Sub TBoxContextMenu_Opened(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles TBoxContextMenu.Opened
        If TBox.CanUndo Then
            UndoMenuItem.IsEnabled = True
        Else
            UndoMenuItem.IsEnabled = False
        End If
        If TBox.CanRedo Then
            RedoMenuItem.IsEnabled = True
        Else
            RedoMenuItem.IsEnabled = False
        End If
        If My.Computer.Clipboard.ContainsText Then
            PasteMenuItem.IsEnabled = True
        Else
            PasteMenuItem.IsEnabled = False
        End If
    End Sub

    Private Sub UndoMenuItem_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles UndoMenuItem.Click
        Me.RaiseEvent(New RoutedEventArgs(UndoEvent))
    End Sub

    Private Sub RedoMenuItem_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles RedoMenuItem.Click
        Me.RaiseEvent(New RoutedEventArgs(RedoEvent))
    End Sub

    Private Sub CutMenuItem_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles CutMenuItem.Click
        Me.RaiseEvent(New RoutedEventArgs(CutEvent))
    End Sub

    Private Sub CopyMenuItem_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles CopyMenuItem.Click
        Me.RaiseEvent(New RoutedEventArgs(CopyEvent))
    End Sub

    Private Sub PasteMenuItem_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles PasteMenuItem.Click
        Me.RaiseEvent(New RoutedEventArgs(PasteEvent))
    End Sub

    Private Sub DeleteMenuItem_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles DeleteMenuItem.Click
        Me.RaiseEvent(New RoutedEventArgs(DeleteEvent))
    End Sub

    Private Sub SelectAllMenuItem_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles SelectAllMenuItem.Click
        Me.RaiseEvent(New RoutedEventArgs(SelectAllEvent))
    End Sub

    Private Sub FindMenuItem_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles FindMenuItem.Click
        Me.RaiseEvent(New RoutedEventArgs(FindEvent))
    End Sub

#End Region

    Private Sub TBox_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles TBox.SelectionChanged
        Me.RaiseEvent(New RoutedEventArgs(UpdateButtons))
    End Sub

    Private Sub TBox_TextChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.TextChangedEventArgs) Handles TBox.TextChanged
        If FileChanged = False Then
            FileChanged = True
        End If
    End Sub

#End Region

End Class